// ignore_for_file: prefer_const_constructors

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'package:junyeyishu/widgets/QGWidget.dart';
import 'widget/QGShippingAddressDialog.dart';
import 'package:junyeyishu/utils/utils.dart';
import 'widget/QGModifyNicknameWidgt.dart';
import 'widget/QGLogOutWidget.dart';
import 'widget/QGSelectAvatarDialogWidget.dart';

class QGMineAccountPage extends StatefulWidget {
  QGMineAccountPage({super.key, this.userInfo});
  Map<String, dynamic>? userInfo;

  @override
  State<QGMineAccountPage> createState() => _QGMineAccountPageState();
}

class _QGMineAccountPageState extends State<QGMineAccountPage> {
  @override
  void initState() {
    super.initState();
    eventBus.on<AvatarEvent>().listen((AvatarEvent data) {
      widget.userInfo?['avatar'] = data.avatar;
      setState(() {});
    });

    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return QGScaffold(
      title: '个人信息',
      bgColor: Color(0XFF0c1012),
      body: Stack(
        children: [
          SizedBox(
            height: kScreenHeight,
            child: SingleChildScrollView(
              child: Column(
                children: [
                  SizedBox(height: 10.rpx),
                  buildAccountInfo(),
                  SizedBox(height: 10.rpx),
                  buildBlockchainInfo(),
                  SizedBox(height: 10.rpx),
                  // inReview() ? Container() : buildAccountAddress(),
                  buildAccountLogoff() 
                  // inReview() ? buildAccountLogoff() : Container(),
                ],
              ),
            ),
          ),
          Positioned(
            left: 10.rpx,
            right: 10.rpx,
            bottom: 34.rpx,
            child: buildButton(),
          ),
        ],
      ),
    );
  }

  buildAccountInfo() {
    return Container(
      margin: EdgeInsets.only(left: 10.rpx, right: 10.rpx),
      padding: EdgeInsets.only(left: 15.rpx, right: 15.rpx),
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(6.rpx), color: Color(0xFF232A32)),
      child: Column(
        children: [
          buildAccountAvatar(),
          buildAccountNickname(),
          buildAccountPhone(),
        ],
      ),
    );
  }

  buildAccountAvatar() {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        showModalBottomSheet(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(20.rpx),
                    topRight: Radius.circular(20.rpx))),
            context: context,
            backgroundColor: Colors.transparent,
            builder: (BuildContext context) {
              return QGSelectAvatar();
            });
      },
      child: Container(
        height: 55.rpx,
        decoration: BoxDecoration(
            border: Border(
                bottom: BorderSide(
                    width: 0.5.rpx,
                    color: Color.fromRGBO(151, 151, 151, 0.2)))),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            buildAccountTltle('头像'),
            Expanded(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  buildAvatarImg(),
                  SizedBox(
                    width: 8.rpx,
                  ),
                  QGImage(asset: 'account_right'),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

  buildAvatarImg() {
    return ClipRRect(
      borderRadius: BorderRadius.circular(30.rpx),
      child: QGImage(
          width: 30.rpx, height: 30.rpx, url: widget.userInfo?["avatar"]),
    );
  }

  buildAccountNickname() {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        showModalBottomSheet(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(20.rpx),
                    topRight: Radius.circular(20.rpx))),
            context: context,
            isScrollControlled: true,
            backgroundColor: Color(0xFF232A32),
            builder: (BuildContext context) {
              return SingleChildScrollView(
                child: Container(
                  padding: EdgeInsets.only(
                      bottom: MediaQuery.of(context).viewInsets.bottom),
                  child: QGModifyMickname(
                    nickname: widget.userInfo!['nickname'],
                  ),
                ),
              );
            }).then((value) {
          if (value != null) {
            widget.userInfo!['nickname'] = value;
            setState(() {});
          }
        });
      },
      child: Container(
        height: 55.rpx,
        decoration: BoxDecoration(
            border: Border(
                bottom: BorderSide(
                    width: 0.5.rpx,
                    color: Color.fromRGBO(151, 151, 151, 0.2)))),
        child:
            Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
          buildAccountTltle('昵称'),
          SizedBox(
            width: 15.rpx,
          ),
          Expanded(
              child: Text(
            widget.userInfo?["nickname"] ?? '',
            textAlign: TextAlign.end,
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
            style: TextStyle(
                fontSize: 13.rpx,
                fontWeight: FontWeight.normal,
                color: Color(0xFF999999)),
          )),
          SizedBox(
            width: 8.rpx,
          ),
          QGImage(asset: 'account_right'),
        ]),
      ),
    );
  }

  buildAccountPhone() {
    return SizedBox(
      height: 55.rpx,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          buildAccountTltle('手机号'),
          Expanded(
              child: Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              Text(
                  '${widget.userInfo?["phone"]}'
                      .replaceFirst(RegExp(r'\d{4}'), '****', 3),
                  style: TextStyle(
                      fontSize: 13.rpx,
                      fontWeight: FontWeight.normal,
                      color: Color(0xFF999999))),
            ],
          ))
        ],
      ),
    );
  }

  buildBlockchainInfo() {
    return Container(
      margin: EdgeInsets.only(left: 10.rpx, right: 10.rpx),
      padding: EdgeInsets.only(left: 15.rpx, right: 15.rpx),
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(6.rpx), color: Color(0xFF232A32)),
      child: Column(
        children: [
          buildBlockchainAddress(),
          // buildLoginPassword(),
          // buildTradePassword(),
          buildAccountAddress()
        ],
      ),
    );
  }

  buildBlockchainAddress() {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        Clipboard.setData(
            ClipboardData(text: '${widget.userInfo?["address"]}'));
        toast('复制成功');
      },
      child: Container(
        height: 55.rpx,
        decoration: BoxDecoration(
            border: Border(
                bottom: BorderSide(
                    width: 0.5.rpx,
                    color: Color.fromRGBO(151, 151, 151, 0.2)))),
        child:
            Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
          buildAccountTltle('区块链地址'),
          Expanded(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                Text(
                    '${widget.userInfo?["address"]}'.replaceRange(
                        8, '${widget.userInfo?["address"]}'.length - 6, '****'),
                    style: TextStyle(
                        fontSize: 13.rpx,
                        fontWeight: FontWeight.normal,
                        color: Color(0xFF999999))),
                SizedBox(
                  width: 8.rpx,
                ),
                QGImage(
                    width: 14.rpx,
                    height: 14.rpx,
                    asset: 'mine_blockchain_copy')
              ],
            ),
          )
        ]),
      ),
    );
  }

  buildLoginPassword() {
    return Container(
      height: 55.rpx,
      decoration: BoxDecoration(
        border: Border(
          bottom: BorderSide(
            width: 0.5.rpx,
            color: Color.fromRGBO(151, 151, 151, 0.2),
          ),
        ),
      ),
      child: GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: () {
          Navigator.of(context).pushNamed(pageMineLoginPassword);
        },
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            buildAccountTltle('登录密码'),
            QGImage(asset: 'account_right'),
          ],
        ),
      ),
    );
  }

  buildTradePassword() {
    return SizedBox(
      height: 55.rpx,
      child: GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: () {
          Navigator.of(context).pushNamed(pageTradePassword);
        },
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            buildAccountTltle('交易密码'),
            QGImage(asset: 'account_right'),
          ],
        ),
      ),
    );
  }

  buildAccountAddress() {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        showModalBottomSheet(
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(20.rpx),
                  topRight: Radius.circular(20.rpx)),
            ),
            context: context,
            isScrollControlled: true,
            backgroundColor: Color(0xFF232A32),
            builder: (BuildContext context) {
              return SingleChildScrollView(
                child: Container(
                    padding: EdgeInsets.only(
                        bottom: MediaQuery.of(context).viewInsets.bottom),
                    child: QGShippingAddressDialog()),
              );
            }).then((value) {
          if (value != null) {
            widget.userInfo!['shipping_address'] = value;
            setState(() {});
          }
        });
      },
      child: Container(
        width: kScreenWidth,
        height: 55.rpx,
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(6.rpx),
            color: Color(0xFF232A32)),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            buildAccountTltle('收货地址'),
            SizedBox(
              width: 15.rpx,
            ),
            Expanded(
              child: Text(widget.userInfo?["shipping_address"] ?? '',
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  textAlign: TextAlign.end,
                  style: TextStyle(
                      fontSize: 13.rpx,
                      fontWeight: FontWeight.normal,
                      color: Color(0xFF999999))),
            ),
            SizedBox(
              width: 8.rpx,
            ),
            QGImage(asset: 'account_right'),
          ],
        ),
      ),
    );
  }

  buildAccountLogoff() {
    return GestureDetector(
      onTap: () {
        Navigator.of(context).pushNamed(pageLogoff);
      },
      child: Container(
        height: 55.rpx,
        margin: EdgeInsets.only(left: 10.rpx, right: 10.rpx, top: 10.rpx),
        padding: EdgeInsets.fromLTRB(15.rpx, 0, 15.rpx, 0),
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(6.rpx),
            color: Color(0xFF232A32)),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            buildAccountTltle('注销账户'),
            QGImage(asset: 'account_right'),
          ],
        ),
      ),
    );
  }

  buildButton() {
    return GestureDetector(
      onTap: () {
        showDialog(
            context: context,
            barrierDismissible: false,
            builder: (BuildContext context) {
              return QGLogOut();
            });
      },
      child: Container(
        width: kScreenWidth,
        height: 44.rpx,
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(10.rpx),
            color: Color(0xFF232A32)),
        child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
          Text(
            '退出账号',
            style: TextStyle(
                fontSize: 14.rpx,
                fontWeight: FontWeight.bold,
                color: Color(0xFFffffff)),
          ),
        ]),
      ),
    );
  }

  buildAccountTltle(String title) {
    return Text(
      title,
      style: TextStyle(
          fontSize: 13.rpx,
          fontWeight: FontWeight.bold,
          color: Color(0xFFffffff)),
    );
  }
}
